/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;
import java.util.*;
import javax.persistence.*;
import play.data.format.*;
import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class Persona {

    @Id
    @Constraints.Required
    public Integer numPersona;
    
    @Constraints.Required
    public String nombres;
    
    @Constraints.Required
    public String apellidos;
    
    @Constraints.Required
    public String docIdentidad;
    
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date fechaNacimiento	;
    
    @Constraints.Required
    public String direccion;
    
    public static Persona findById(Integer numPersona) {
        return JPA.em().find(Persona.class, numPersona);
    }
    
    public void update(Integer numPersona) {
        this.numPersona = numPersona;
        JPA.em().merge(this);
    }
    
    public void save() {
        JPA.em().persist(this);
    }
    
    public void delete() {
        JPA.em().remove(this);
    }
    
    public static Page page() {
        @SuppressWarnings("unchecked")
            List<Persona> data = JPA.em()
            .createQuery("from Persona p ")
            .getResultList();

        
        Long total=Long.parseLong(data.size()+"");
        return new Page(data, total, 1, 10);
    }
    
     public static class Page {
        
        private final int pageSize;
        private final long totalRowCount;
        private final int pageIndex;
        private final List<Persona> list;
        
        public Page(List<Persona> data, long total, int page, int pageSize) {
            this.list = data;
            this.totalRowCount = total;
            this.pageIndex = page;
            this.pageSize = pageSize;
        }
        
        public long getTotalRowCount() {
            return totalRowCount;
        }
        
        public int getPageIndex() {
            return pageIndex;
        }
        
        public List<Persona> getList() {
            return list;
        }
        
        public boolean hasPrev() {
            return pageIndex > 1;
        }
        
        public boolean hasNext() {
            return (totalRowCount/pageSize) >= pageIndex;
        }
        
        public String getDisplayXtoYofZ() {
            int start = ((pageIndex - 1) * pageSize + 1);
            int end = start + Math.min(pageSize, list.size()) - 1;
            return start + " al " + end + " de " + totalRowCount;
        }
        
    }
}
